package com.dhcc.core.modules.system.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dhcc.core.framework.annotion.BussinessLog;
import com.dhcc.core.framework.annotion.CacheClear;
import com.dhcc.core.framework.annotion.Permission;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.constant.state.PositionType;
import com.dhcc.core.framework.exception.BizException;
import com.dhcc.core.framework.exception.BizExceptionEnum;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.node.ZTreeNode;
import com.dhcc.core.framework.util.CommonUtil;
import com.dhcc.core.modules.system.cache.group.IGroupCache;
import com.dhcc.core.modules.system.cache.position.PositionCache;
import com.dhcc.core.modules.system.entity.Position;
import com.dhcc.core.modules.system.service.IPositionService;
import com.dhcc.core.modules.system.warpper.PositionWarpper;



/**
 * 职务
 * 
 * @ClassName: PositionController
 * @Description: TODO
 * @author mk
 * @date 2018-08-28 08:52:01
 */
@Controller
@RequestMapping("${sys.backendPath}/position")
public class PositionController extends BaseController {

	private String PREFIX = "/backend/system/position/";

	@Autowired
	private IPositionService positionService;
	
    @Value("${sys.orgFullName}")
    private String orgFullName;

	

    /**
     * 跳转到职务列表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        model.addAttribute("positiontype", PositionType.map());
        return PREFIX + "position.html";
    }
	
    /**
     * 跳转到添加职务
     */
    @Permission("position:add")
    @RequestMapping("/to_add")
    public String positionAdd(Model model) {
        model.addAttribute("positiontype",PositionType.map());
        return PREFIX + "position_add.html";
    }
	
	/**
     * 跳转到修改职务
     */
    @Permission("position:update")
    @RequestMapping("/to_update/{positionId}")
    public String positionUpdate(@PathVariable Long positionId, Model model) {
        Position position = this.positionService.selectById(positionId);
        model.addAttribute("position",position);
        model.addAttribute("positiontype",PositionType.map());
        return PREFIX + "position_edit.html";
    }

    /**
     * 获取职务列表
     */
	@SuppressWarnings("unchecked")
    @RequestMapping(value = "/list")
    @Permission("position:list")
    @ResponseBody
    public Object list(@RequestParam Map<String, Object> params) {
        List<Position> result = this.positionService.list(params);
        for (PositionType pt : PositionType.values()) {
            Position p = new Position();
            p.setId(pt.getCode() + 10000L);
            p.setName(pt.getName());
            result.add(p);
        }
        List<Map<String, Object>> warp = (List<Map<String, Object>>) new PositionWarpper(result).warp();
        return warp;
    }

    /**
     * 新增职务
     */
    @RequestMapping(value = "/add")
    @Permission("position:add")
    @ResponseBody
    @BussinessLog(value = "新增职务")
    @CacheClear(IGroupCache.CACHE_NAME)
    public Object add(Position position) {
        if (CommonUtil.isOneEmpty(position)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        position.insert();
        return R.ok();
    }

    /**
     * code验证
     */
    @RequestMapping(value = "/codevalidate")
    @ResponseBody
    @BussinessLog(value = "code验证")
    public Map<String, Object> codevalidate(@RequestParam String code) {
        Map<String, Object> map = new HashMap<>();
        Boolean msg = this.positionService.codevalidate(code);
        map.put("msg",msg);
        return map;
    }

    @RequestMapping(value = "/delete")
    @Permission("position:delete")
    @ResponseBody
    @BussinessLog(value = "删除职务")
    @CacheClear(IGroupCache.CACHE_NAME)
    public Object delete(@RequestParam Long positionId) {

        this.positionService.deleteById(positionId);

        return R.ok();
    }

    /**
     * 修改职务
     */
    @RequestMapping(value = "/update")
    @Permission("position:update")
    @ResponseBody
    @BussinessLog(value = "修改职务")
    @CacheClear(IGroupCache.CACHE_NAME)
    public Object update(Position position) {
        if (CommonUtil.isOneEmpty(position)) {
            throw new BizException(BizExceptionEnum.REQUEST_NULL);
        }
        Position old = this.positionService.selectById(position.getId());

        old.setCode(position.getCode());
        old.setName(position.getName());
        old.setDescription(position.getDescription());
        old.setLevels(position.getLevels());
        old.setWeight(position.getWeight());
        old.updateById();
        return R.ok();
    }

    @RequestMapping(value = "/tree")
    @ResponseBody
    public List<ZTreeNode> tree() {
        return PositionCache.me().getPositionTree();
    }
	
}
